/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2014-08-22 11:25:55
 * @filename : static/mnm/src/views/forgetPassword.js
 * @version : 
 * @description : 
 */

define(function(require, exports, module){

        require("{resourceUrl}css/mnm/public.css");

        var FgPWView,
            $ = require('$'),
	    Backbone = require('backbone'),
            _ = require('underscore'),
            util = require('{baseSea}util/util'),
            share = require('../share'),
            lang = require('{baseSea}I18N/mnm/{locale}'),
            tpl = require('{resourceUrl}tpl/mnm/forgetPassword.tpl');
	
        FgPWView = Backbone.View.extend({
                   
		el: 'body',
		
                //el: '#forgetPassword',
 
		template: _.template(tpl),
                
                /*
		events: {
                    "tap .back"         : "goBack",  
                    "tap .btn-forgetpw" : "sendEmail" 	        
                },
                */

		initialize: function () {
                },

                bindEvt : function() {
                    var dom = $('#forgetPassword'),tthis = this;
                
                    dom.find('.back').tap(function(){
                        tthis.goBack();
                    });

                    dom.find('.btn-forgetpw').tap(function(){
                        tthis.sendEmail();
                    });
                },

		render: function (obj) {
                    if ($('#forgetPassword').length < 1) { 
                        $('body').append(this.template(lang)); 
                        this.bindEvt(); 
                    } else {
		        $('#forgetPassword').show();
                    }
                    $('#body_loading').hide(); 
                },

                sendEmail : function() {
                    var obj = {};
                        obj.email = $('input[name="email"]').val();
			if (this.validata(obj)) {
			    this.ajaxSendEmail(obj,this);
			}
                },

                goBack : function() {
                     window.location.href = '#signin/whole'; 
                },
 
                validata : function(data) {
		     var obj = {bool:true,type:1};
		     if ($.trim(data.email) == '') {
		         obj.bool = false;
		     } else if (!util.isEmail(data.email)) {
			 obj.bool = false;
		     }
		     obj.dom = $('.email');
		     obj.msg = lang.forgetpw_email;
		     share.inputError(obj);
		     return obj.bool; 
		},
 
                ajaxSendEmail : function() {
                     var dom = $('.btn-forgetpw');
                         dom.attr('ajax',1);
		         share.ajaxStartA(dom);
		     var obj = {
			 url : seajs.data.vars.apiUrl+'user_register',
			 type : 'POST',
			 data : data,
			 success : function(data) {
				 if (data.error == 0 ) {
				     alert('send email success!');
				 } else {
				     alert('error');
				 } 
			 },
			 error : function(xhr, errorType, error) {
				 share.error('alert','http request failed!'); 
			 },
			 complete : function(xhr,status){
			     dom.attr('ajax',0);
			     share.ajaxEndA(dom);
			 } 
		    };
                    //$.ajax(util.ajaxControl(obj)); 
                }
 
	});

	module.exports = FgPWView;
});

